package com.zq.it.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zq.it.pojo.Page;

public class PagerTag extends TagSupport {

    private static final long serialVersionUID = 5729832874890369508L;

    private Page page;

    @Override
	public int doStartTag() throws JspException {
        StringBuilder sb = new StringBuilder();
        sb.append("<div class=\"pagination\">\r\n");
        if (page.getTotalCount() == 0) {
            sb.append("<em style=\"color:red\">没有可显示的数据！</em>\r\n");
        } else {
            sb.append("<form method=\"post\" action=\"").append(page.getUrl())
                    .append("\" name=\"qPagerForm\">\r\n");
            sb.append("<input id=\"currentPageId\" type=\"hidden\" name=\"")
                    .append("pageConfig.currentPage").append("\" value=\"")
                    .append(page.getCurrentPage()).append("\"/>\r\n");
            sb.append(" 共<strong>").append(page.getTotalCount())
                    .append("</strong>项").append("/<strong>")
                    .append(page.getPageCount()).append("</strong>页: \r\n");

            if (page.getCurrentPage() == 1)
                sb.append("<label disabled>«上一页</label>\r\n");
            else {
                sb.append("<span onclick=\"turnOverPage(")
                        .append(page.getCurrentPage() - 1)
                        .append(")\">«上一页</span>\r\n");
            }
            int start = 1;
            if (page.getCurrentPage() > 4) {
                start = page.getCurrentPage() - 1;
                sb.append("<span onclick=\"turnOverPage(1)\">1</span>\r\n");
                sb.append("<span onclick=\"turnOverPage(2)\">2</span>\r\n");
                sb.append("…\r\n");
            }

            int end = page.getCurrentPage() + 1;
            if (end > page.getPageCount()) {
                end = page.getPageCount();
            }
            for (int i = start; i <= end; i++) {
                if (page.getCurrentPage() == i)
                    sb.append("<span class=\"current\">").append(i)
                            .append("</span>\r\n");
                else {
                    sb.append("<span onclick=\"turnOverPage(").append(i)
                            .append(")\">").append(i).append("</span>\r\n");
                }
            }

            if (end < page.getPageCount() - 2) {
                sb.append("…\r\n");
            }
            if (end < page.getPageCount() - 1) {
                sb.append("<span onclick=\"turnOverPage(")
                        .append(page.getPageCount() - 1).append(")\">")
                        .append(page.getPageCount() - 1).append("</span>\r\n");
            }
            if (end < page.getPageCount()) {
                sb.append("<span onclick=\"turnOverPage(")
                        .append(page.getPageCount()).append(")\">")
                        .append(page.getPageCount()).append("</span>\r\n");
            }

            if (page.getCurrentPage() == page.getPageCount())
                sb.append("<label disabled>下一页 »</label>\r\n");
            else {
                sb.append("<span onclick=\"turnOverPage(")
                        .append(page.getCurrentPage() + 1)
                        .append(")\">下一页 »</span>\r\n");
            }
            sb.append("</form>\r\n");

            sb.append("<script language=\"javascript\">\r\n");
            sb.append("function turnOverPage(no){\r\n");
            sb.append("if(no>").append(page.getPageCount()).append("){");
            sb.append("no=").append(page.getPageCount()).append(";}\r\n");
            sb.append("if(no<1){no=1;}\r\n");
            sb.append("document.qPagerForm.currentPageId.value=no;\r\n");
            sb.append("document.qPagerForm.submit();\r\n");
            sb.append("}\r\n");
            sb.append("</script>\r\n");
        }
        sb.append("</div>\r\n");
        try {
            pageContext.getOut().println(sb.toString());
        } catch (IOException e) {
            throw new JspException(e);
        }
        return 0;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

}